package com.zf.lease.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;

import com.zf.apartment.mapper.RoomMapper;

import com.zf.system.mapper.CustomerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import com.zf.lease.mapper.OrderMsgMapper;
import com.zf.lease.domain.OrderMsg;
import com.zf.lease.service.IOrderMsgService;

/**
 * 预约信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-08
 */
@Service
public class OrderMsgServiceImpl extends ServiceImpl<OrderMsgMapper, OrderMsg> implements IOrderMsgService {
    @Autowired
    private OrderMsgMapper orderMsgMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private RoomMapper roomMapper;


    /**
     * 根据联合主键查询单个预约信息
     */
    public OrderMsg selectOrderMsgByKey(Long customerId,Long roomId) {
        OrderMsg orderMsg = new OrderMsg();
        orderMsg.setCustomerId(customerId);
        orderMsg.setRoomId(roomId);
        return orderMsgMapper.selectOrderMsgByKey(orderMsg);
    }

    /**
     * 查询预约信息
     *
     * @param customerId 预约信息主键
     * @return 预约信息
     */
    @Override
    public OrderMsg selectOrderMsgByCustomerId(Long customerId) {
        return orderMsgMapper.selectOrderMsgByCustomerId(customerId);
    }

    /**
     * 查询预约信息列表
     *
     * @param orderMsg 预约信息
     * @return 预约信息
     */
    @Override
    public List<OrderMsg> selectOrderMsgList(OrderMsg orderMsg) {
        return orderMsgMapper.selectOrderMsgList(orderMsg);
    }

    /**
     * 新增预约信息
     *
     * @param orderMsg 预约信息
     * @return 结果
     */
    @Override
    public int insertOrderMsg(OrderMsg orderMsg) {
            Long customerId = customerMapper.selectCustomerIdByCustomerName(orderMsg.getCustomerName());
            orderMsg.setCustomerId(customerId);
            Long roomId = roomMapper.selectRoomByRoomName(orderMsg.getRoomName());
            orderMsg.setRoomId(roomId);
            try {
                // 执行插入操作
                return orderMsgMapper.insertOrderMsg(orderMsg); // 假设返回插入的记录数
            } catch (DataIntegrityViolationException e) {
                // 捕获重复条目的异常
                return 0; // 返回特定的值表示失败
            } catch (Exception e) {
                // 捕获其他异常
                System.err.println("插入失败: " + e.getMessage());
                return -1; // 返回特定的值表示失败
            }

    }

    /**
     * 修改预约信息
     *
     * @param orderMsg 预约信息
     * @return 结果
     */
    @Override
    public int updateOrderMsg(OrderMsg orderMsg) {
        Long customerId = customerMapper.selectCustomerIdByCustomerName(orderMsg.getCustomerName());
        orderMsg.setCustomerId(customerId);
        Long roomId = roomMapper.selectRoomByRoomName(orderMsg.getRoomName());
        orderMsg.setRoomId(roomId);
        return orderMsgMapper.updateOrderMsg(orderMsg);
    }

    /**
     * 批量删除预约信息
     *
     * @param customerIds 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteOrderMsgByCustomerIds(Long[] customerIds) {
        return orderMsgMapper.deleteOrderMsgByCustomerIds(customerIds);
    }

    /**
     * 删除预约信息信息
     *
     * @param customerId 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteOrderMsgByCustomerId(Long customerId) {
        return orderMsgMapper.deleteOrderMsgByCustomerId(customerId);
    }

    @Override
    public int deleteOrderMsgByKey(Long[] customerId, Long[] roomId) {
        // 检查数组是否为空并确保长度一致
        if (customerId == null || roomId == null || customerId.length != roomId.length) {
            throw new IllegalArgumentException("customerId 和 roomId 数组不能为空且长度必须一致");
        }

        int totalDeleted = 0; // 记录删除的总数

        // 遍历数组，创建 OrderMsg 对象并删除
        for (int i = 0; i < customerId.length; i++) {
            OrderMsg orderMsg = new OrderMsg();
            orderMsg.setCustomerId(customerId[i]); // 设置当前的 customerId
            orderMsg.setRoomId(roomId[i]); // 设置当前的 roomId

            // 调用 Mapper 方法删除数据
            totalDeleted += orderMsgMapper.deleteOrderMsgByKey(orderMsg);
        }

        return totalDeleted; // 返回删除的总数
    }
}